<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class GoodsController extends Controller
{
	public function index(Request $request){
	    $where = [];
	    //实例化要操作的表
	    $db = DB::table("goods")->join("type",'goods.tid','=','type.id');
	    //判断有没有搜索name字段的值
	    if($request->has('name')){
	        // 获取要搜索的name字段的值
	        $name = $request->input('name');
	        //添加where模糊匹配条件
	        $db->where('name','like',"%{$name}%");
	        $where['name'] = $name;
	    }
	    // 分页
	    $list = $db->paginate(8);
	    //查询type表
	    //加载模板
	    return view('admin.goods.goods',['list'=>$list,'where'=>$where]);
	}

	public function create()
	{
        $type = DB::table('type')->select(DB::raw('*,concat(path,id) as pathid'))->orderBy('pathid')->get();
        return view('admin.goods.add',['type'=>$type]);
	}
	public function store(Request $request)
	{
		$messages=[
			'required'=>':attribute字段填写错误',
		];
		$this->validate($request, [
			'goodname'=>'required|unique:goods',
		],$messages);
		$data=$request->except('_token');
		$id=DB::table('goods')->insertGetId($data);
		if ($id>0) {
			return view('admin.goods.pic');
		}
	}

    public function destroy($id)
    {
    	$row = DB::table("goods")->where("id",$id)->delete();
    	if($row>0){
    		return back();
    	}
    }

}
